goog.provide('ptucker.ChatAdmin');

goog.require('goog.Uri.QueryData');
goog.require('goog.debug.FancyWindow');
goog.require('goog.debug.Logger');
goog.require('goog.dom');
goog.require('goog.json');
goog.require('goog.net.EventType');
goog.require('goog.net.XhrIo');
goog.require('goog.structs.Set');

/**
 * @param {?string=} opt_baseUrl Base URL.
 * @constructor
 */
ptucker.ChatAdmin = function(opt_baseUrl) {
  /**
   * @type {string}
   * @private
   */
  this.baseUrl_ = opt_baseUrl || '';

  /**
   * @type {goog.debug.FancyWindow}
   * @private
   */
  this.debugWindow_ = new goog.debug.FancyWindow('ptucker.ChatAdmin');

  this.debugWindow_.setEnabled(true);
  this.debugWindow_.init();
};

/**
 * @type {goog.debug.Logger}
 * @private
 */
ptucker.ChatAdmin.prototype.logger_ =
    goog.debug.Logger.getLogger('ptucker.ChatAdmin');

/**
 * Mockable for tests.
 * @return {goog.net.XhrIo} New XHR object.
 */
ptucker.ChatAdmin.prototype.newXhr_ = function() {
  return new goog.net.XhrIo();
};

/**
 * @param {goog.Uri.QueryData} queryData Parameters.
 * @param {Function(object)} callback Response callback.
 * @private
 */
ptucker.ChatAdmin.prototype.request_ = function(path, queryData, callback) {
  this.logger_.info('request_');

  var xhr = this.newXhr_();
  xhr.addEventListener(
      [goog.net.EventType.COMPLETE, goog.net.EventType.TIMEOUT],
      goog.bind(this.onResponse_, this, callback));
  xhr.send(this.baseUrl_ + path, 'POST', queryData.toString());
};

/**
 * @param {Function(object)} callback Response callback.
 * @param {goog.net.Event} event Response object.
 * @private
 */
ptucker.ChatAdmin.prototype.onResponse_ = function(callback, event) {
  this.logger_.info('onResponse_');
  var response = null;

  var xhr = event.target;
  if (goog.net.EventType.COMPLETE == event.type && xhr && xhr.isSuccess()) {
    response = goog.json.parse(xhr.getResponseText());
  } else {
    this.logger_.severe('response error type|xhr=' + event.type + '|' +
        (xhr && xhr.getStatus()));
  }

  if (xhr) {
    xhr.dispose();
  }

  callback(response);
};

/**
 * @param {string} name Room name.
 * @param {Function(object)} callback Response callback.
 */
ptucker.ChatAdmin.prototype.newRoom = function(name, callback) {
  var queryData = new goog.Uri.QueryData();
  queryData.set('name', name);
  this.request_('/newroom', queryData, callback);
};

/**
 * @param {number} id Room ID.
 * @param {Function(object)} callback Response callback.
 */
ptucker.ChatAdmin.prototype.deleteRoom = function(id, callback) {
  var queryData = new goog.Uri.QueryData();
  queryData.set('id', id);
  this.request_('/deleteroom', queryData, callback);
};

/**
 * @param {?string} creatorEmail Room creator.
 * @param {Function(object)} callback Response callback.
 */
ptucker.ChatAdmin.prototype.roomList = function(creatorEmail, callback) {
  var queryData = new goog.Uri.QueryData();
  if (creatorEmail) {
    queryData.set('creatorEmail', creatorEmail);
  }
  this.request_('/roomList', queryData, callback);
};
